package com.fescoservice.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.fescoservice.config.PropertiesConfig;
import com.fescoservice.exception.FSException;
import com.fescoservice.model.Operator;

/**
 * A data access object (DAO) providing persistence and search support for
 * Operator entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.fescoservice.dao.Operator
 * @author MyEclipse Persistence Tools
 */

public class OperatorDAO extends HibernateDaoSupport {
	private static final Logger log = LogManager.getLogger(OperatorDAO.class);
	// property constants
	public static final String OPER_NAME = "operName";
	public static final String OPER_TYPE = "operType";
	public static final String OPER_ROLE_ID = "operRoleId";
	public static final String OPER_COMPANY_ID = "operCompanyId";
	public static final String OPER_DEPT_ID = "operDeptId";
	public static final String OPER_STATUS = "operStatus";
	public static final String OPER_EMAIL = "operEmail";
	public static final String OPER_TEL = "operTel";

	protected void initDao() {
		// do nothing
	}

	public void save(Operator transientInstance) {
		log.debug("saving Operator instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Operator persistentInstance) {
		log.debug("deleting Operator instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Operator findById(java.lang.String id) {
		log.debug("getting Operator instance with id: " + id);
		try {
			Operator instance = (Operator) getHibernateTemplate().get(
					"com.fescoservice.model.Operator", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Operator instance) {
		log.debug("finding Operator instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Operator instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Operator as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByOperName(Object operName) {
		return findByProperty(OPER_NAME, operName);
	}

	public List findByOperType(Object operType) {
		return findByProperty(OPER_TYPE, operType);
	}

	public List findByOperRoleId(Object operRoleId) {
		return findByProperty(OPER_ROLE_ID, operRoleId);
	}

	public List findByOperCompanyId(Object operCompanyId) {
		return findByProperty(OPER_COMPANY_ID, operCompanyId);
	}

	public List findByOperDeptId(Object operDeptId) {
		return findByProperty(OPER_DEPT_ID, operDeptId);
	}

	public List findByOperStatus(Object operStatus) {
		return findByProperty(OPER_STATUS, operStatus);
	}

	public List findByOperEmail(Object operEmail) {
		return findByProperty(OPER_EMAIL, operEmail);
	}

	public List findByOperTel(Object operTel) {
		return findByProperty(OPER_TEL, operTel);
	}

	public List findAll() {
		log.debug("finding all Operator instances");
		try {
			String queryString = "from Operator";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Operator merge(Operator detachedInstance) {
		log.debug("merging Operator instance");
		try {
			Operator result = (Operator) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Operator instance) {
		log.debug("attaching dirty Operator instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Operator instance) {
		log.debug("attaching clean Operator instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public Long countOperatorsById(final String sql,final String id) throws FSException{
		log.debug("finding Category instance by example");
		try {
			Long count = (Long) getHibernateTemplate().execute(
					   new HibernateCallback() {
					   public Object doInHibernate(Session session)
					   throws HibernateException, SQLException {						   
						   SQLQuery q = session.createSQLQuery(sql);
						   q.setString("operid",id);
						  return q.addScalar( "cnt" , Hibernate.LONG).uniqueResult();
					   }
					 } );
			return count;
		} catch (Exception re) {
			log.error("count categories failed", re);
			throw new FSException("FS_ERROR_COUNTCATGORY",re.getMessage());
		}
	}
	
	public Long countOperators(final String sql,final String type) throws FSException{
		log.debug("finding Category instance by example");
		try {
			Long count = (Long) getHibernateTemplate().execute(
					   new HibernateCallback() {
					   public Object doInHibernate(Session session)
					   throws HibernateException, SQLException {						   
						   SQLQuery q = session.createSQLQuery(sql);
						   if(null!=type){
							   q.setString("type",type);
						   }
						  return q.addScalar( "cnt" , Hibernate.LONG).uniqueResult();
					   }
					 } );
			return count;
		} catch (Exception re) {
			log.error("count categories failed", re);
			throw new FSException("FS_ERROR_COUNTCATGORY",re.getMessage());
		}
	}
	
	public List listOperators(final String sql,final String type,final int first) throws FSException{
		log.debug("finding Category instance by example");
		try {
			List result = (List) getHibernateTemplate().execute(
					   new HibernateCallback() {
					   public Object doInHibernate(Session session)
					   throws HibernateException, SQLException {						   
						   Query q = session.createQuery(sql);
						   if(null!=type){
							   q.setString("type",type);
						   }
						   q.setFirstResult(first);
						   q.setMaxResults(PropertiesConfig.getInstance().getInt("ListSize.Operator"));
						  return q.list();
					   }
					 } );
			return result;
		} catch (Exception re) {
			log.error("count categories failed", re);
			throw new FSException("FS_ERROR_COUNTCATGORY",re.getMessage());
		}
	}
	
	public static OperatorDAO getFromApplicationContext(ApplicationContext ctx) {
		return (OperatorDAO) ctx.getBean("OperatorDAO");
	}
	
	
	/**
	 * 根据Id查询Company
	 * @param hql
	 * @param compId
	 * @return
	 * @throws FSException
	 */
	public List hrLogin(final String hql,final String userId,final String userPass)  throws FSException{
		try {
			List rs = (List) getHibernateTemplate().execute(
					   new HibernateCallback() {
					   public Object doInHibernate(Session session)
					   throws HibernateException, SQLException {
						   Query q = session.createQuery(hql);
						   q.setString("userId" , userId); 	
						   q.setString("userPass" , userPass); 	
						   log.debug("~~~~~~~~~hrLogin L272~~~~~~~~~~~~~~~~~`````"); 
						  return q.list();
					   }
					 } );
			return rs;
		} catch (RuntimeException re) {
			log.error("hrLogin failed", re);
			throw re;
		}
	}
	
	
	
	
	
	/**
	 * 根据Id查询Company
	 * @param hql
	 * @param compId
	 * @return
	 * @throws FSException
	 */
	public List queryHRLogin(final String hql,final String userId)  throws FSException{
		try {
			List rs = (List) getHibernateTemplate().execute(
					   new HibernateCallback() {
					   public Object doInHibernate(Session session)
					   throws HibernateException, SQLException {
						   Query q = session.createQuery(hql);
						   q.setString("userId" , userId); 	
						   log.debug("~~~~~~~~~custLogin L302~~~~~~~~~~~~~~~~~`````"); 
						  return q.list();
					   }
					 } );
			return rs;
		} catch (RuntimeException re) {
			log.error("queryHRLogin failed", re);
			throw re;
		}
	}
	
	
	
	
	
}